package cn.itcast.controller;

import cn.itast.domain.Product;
import cn.itcast.service.IProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    private Product product;

    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
                                @RequestParam(name = "size",required = true,defaultValue = "4")Integer size){
        ModelAndView mv =new ModelAndView();
        List<Product> products = productService.findAll(page, size);
        PageInfo pageInfo =new PageInfo(products);

        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("product-list1");

        return mv;
    }

    @RequestMapping("/save")
    public String save(Product product){
        productService.save(product);
        return "redirect:findAll.do";
    }

    @RequestMapping("/update")
    public String update(Product product){
        productService.updateProduct(product);
        return "redirect:findAll.do";
    }
}
